home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / misc / pclta-1.000 / pclta-1 / hostappl / hauif.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-27  |  5.9 KB  |  198 lines

  1. /*
  2.  * hauif.c,v 1.1 1996/05/27 16:07:34 miksic Exp
  3.  */
  4.  
  5. /* HAUIF.C -- Example LONWORKS host application user interface code.
  6.  * Copyright (c) 1993 by Echelon Corporation.
  7.  * All Rights Reserved.
  8.  */
  9.  
  10. #include <ctype.h>                  /* For toupper()                        */
  11. #include <stdlib.h>                 /* For exit()                   */
  12. #include <string.h>                 /* For strlen(), strncpy()              */
  13. #include <stdio.h>                  /* For printf()                         */
  14. #include "conio.h"                  /* for kbhit( ) and getch( )            */
  15. #include "ni_msg.h"
  16. #include "ni_mgmt.h"
  17. #include "hauif.h"
  18.  
  19. #define BUF_SIZE 80
  20.  
  21. /*************************** Function Prototypes ****************************/
  22.  
  23.  
  24. /*  Command interpreter function handlers */
  25.  
  26. void    exit_func( void );
  27. void    verbose( void );
  28. extern  void    traffic( void );
  29. extern  void    NV_table( void );
  30. extern  void    NV_update( void );
  31. extern  void    NV_poll( void );
  32. void    null_cmd( void ) { }
  33.  
  34. typedef struct  {
  35.     char    letter;                 /* command letter */
  36.     void    ( * func )( void );     /* handler function */
  37.     char    * help_text;
  38. } command_struct;
  39.  
  40. const static command_struct command_table[ ] = {
  41.  
  42. {     'E', exit_func,   "(E)xit this application and return to shell"     },
  43. {     'N', NV_table,    "(N)etwork Variable configuration table",         },
  44. {     'P', NV_poll,     "(P)oll input network variable"                   },
  45. {     'T', traffic,     "Incoming network (T)raffic summary"              },
  46. {     'U', NV_update,   "(U)pdate network variable"                       },
  47. {     'V', verbose,     "Control (V)erbose modes"                         },
  48. {     '\r', null_cmd,   ""                                                },
  49. {     '\0' }
  50. };
  51.  
  52. /*
  53.  ****************************************************************************
  54.  * process_cmd().  Process a command from the keyboard
  55.  ****************************************************************************
  56.  */
  57.  
  58. void process_cmd( void )
  59. {
  60.     char ch;
  61.     const command_struct * cmd_ptr;
  62.  
  63.     ch = getch( );        // read a character from keyboard
  64.     ch = toupper( ch );
  65.  
  66.     for( cmd_ptr = command_table; cmd_ptr->letter; cmd_ptr++ ) {
  67.         if( cmd_ptr->letter == ch ) {
  68.             printf( "%s\n", cmd_ptr->help_text );   // echo command
  69.             ( cmd_ptr->func ) ( );        // Dispatch command
  70.             return;
  71.         }
  72.     }
  73.         // List all the commands
  74.  
  75.     printf("\nDidn't recognize that command, valid commands are:\n" );
  76.     for( cmd_ptr = command_table; cmd_ptr->letter != '\r';  cmd_ptr++ )
  77.         printf( "%c -- %s.\n", cmd_ptr->letter, cmd_ptr->help_text );
  78.  
  79.     return;
  80. }
  81.  
  82. /*
  83.  ****************************************************************************
  84.  * verbose mode command
  85.  ****************************************************************************
  86.  */
  87.  
  88.  
  89. void verbose( void ) {
  90.             /* Control verbose and report modes. */
  91.     extern boolean verbose_flag, report_flag;
  92.  
  93.     verbose_flag = get_choice(
  94.         "Display network interface msgs (Y/N) ?",
  95.         'Y', 'N' );
  96.     report_flag = get_choice(
  97.         "Report incoming appl msgs (Y/N) ?",
  98.         'Y', 'N' );
  99. }
  100.  
  101. /*
  102.  ****************************************************************************
  103.  *
  104.  *      get_integer - prompt and read an integer from the command line
  105.  *
  106.  ****************************************************************************
  107.  */
  108.  
  109. int get_integer( const char * prompt, int default_val ) {
  110.     char input_buf[ BUF_SIZE ];
  111.     int number;
  112.  
  113.     for( ;;) {
  114.         printf( prompt );
  115.         printf( " [%d] \t:", default_val );
  116.         gets( input_buf );                  // read a line from keyboard
  117.         if( !input_buf[ 0 ] ) return default_val;   // Empty line
  118.         if( sscanf( input_buf, "%u", &number ) == 1 ) return number;
  119.         printf( "Invalid integer\n" );
  120.     }
  121. }
  122.  
  123. /*
  124.  ****************************************************************************
  125.  *
  126.  *      get_choice - prompt and read a binary choice from the command line
  127.  *
  128.  ****************************************************************************
  129.  */
  130.  
  131. boolean get_choice( const char * prompt, char true_choice, char false_choice ) {
  132.         // default choice is FALSE
  133.     char ch;
  134.  
  135.     for( ;; ) {
  136.         printf( prompt );
  137.         ch = getch( );
  138.         ch = toupper( ch );      // read a character from keyboard
  139.         if( ch == '\r'|| ch == toupper( false_choice ) ) {
  140.             printf( "%c\n", false_choice );
  141.             return FALSE;
  142.         }
  143.         if( ch == toupper( true_choice ) ) {
  144.             printf( "%c\n", true_choice );
  145.             return TRUE;
  146.         }
  147.         printf( "Invalid choice\n" );
  148.     }
  149. }
  150.  
  151. /*
  152.  ****************************************************************************
  153.  *
  154.  *      sign-on message
  155.  *
  156.  ****************************************************************************
  157.  */
  158.  
  159. void sign_on( void ) {
  160.  
  161.     const command_struct * cmd_ptr;
  162.  
  163.     static const char sign_on_message[] = {
  164.       "Welcome to the LONWORKS host application.\n\n"
  165.       "This program acts as an application node\n"
  166.       "Enter one of the following commands by typing the indicated letter:\n\n"
  167.     };
  168.  
  169.     /* Display sign-on message. */
  170.     printf( sign_on_message );
  171.     for( cmd_ptr = command_table; cmd_ptr->letter != '\r';  cmd_ptr++ )
  172.         printf( "%c -- %s.\n", cmd_ptr->letter, cmd_ptr->help_text );
  173. }
  174.  
  175. /*
  176.  ****************************************************************************
  177.  *
  178.  *      error_exit - handle invocation errors
  179.  *
  180.  ****************************************************************************
  181.  */
  182.  
  183. void error_exit( void ) {
  184.  
  185.     static const char flag_message[] = {
  186.       "\n\n"
  187.       "To invoke the LONWORKS Host Application Demonstration Program, enter:"
  188.       "\n\n"
  189.       "    ha [-Ddevice] [-V]\n\n"
  190.       "    -D selects the network device name, the default is 'LON1'.\n"
  191.       "    -V invokes verbose mode.\n"
  192.     };
  193.  
  194.     printf( flag_message );
  195.     exit( 1 );
  196. }
  197.  
  198.